package com.gxa.controller;


import com.fasterxml.jackson.annotation.JsonProperty;
import com.gxa.pojo.AddOrderData;
import com.gxa.service.AddOrderDataService;
import com.gxa.service.XsDdService;
import com.gxa.utils.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description: TOOD
 * @ClassName: AddOrderDataController
 * @Author: ${陈柯宇}
 * Date: 2020/9/25 22:41
 * @Version: 1.0
 */
@Controller
@RequestMapping("/aod")
@Api(value = "",tags = "")
@CrossOrigin
public class AddOrderDataController {

    @Resource
    AddOrderDataService addOrderDataService;

    @Resource
    XsDdService xsDdService;

    /**
     * 选择商品分页数据
     * @param map
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/findAllByPage",method = RequestMethod.POST)
    @ApiOperation(value = "查询选择商品列表分页接口",notes = "获取所有商品数据列表",httpMethod = "post",response = String.class)
    public Map findAllByPage(@RequestBody Map map){
        JsonResult jsonResult =new JsonResult();
        try {
            String number= map.get("number").toString();
            String size= map.get("size").toString();
            Integer offset =(Integer.parseInt(number)-1)*Integer.parseInt(size);
            Integer limit=Integer.parseInt(size);
            List<AddOrderData> byPage = addOrderDataService.findAllByPage(offset, limit);
            jsonResult.setCode("200");
            jsonResult.setMsg("检索成功");
            jsonResult.setData(byPage);
        }catch (Exception e){
            jsonResult.setCode("501");
            jsonResult.setMsg("系统开小差了");
        }
        return jsonResult.getValues();
    }


    /**
     * 商品订单多条件查询 分页
     * @param map
     * @return
     */
    @JsonProperty
    @ResponseBody
    @RequestMapping(value = "/selectByAodLike",method = RequestMethod.POST)
    public Map<String,Object> selectByAodLike(@RequestBody Map<String,Object> map){
        JsonResult jsonResult=new JsonResult();
       try {
           String number= map.get("number").toString();
           String size= map.get("size").toString();
           Integer offset =(Integer.parseInt(number)-1)*Integer.parseInt(size);
           Integer limit=Integer.parseInt(size);
           map.put("offset",offset);
           map.put("limit",limit);
           List<Map<String, Object>> byAodLike = addOrderDataService.selectByAodLike(map);
           Integer count = addOrderDataService.selectByAodLikeCount(map);
           jsonResult.put("count",count);
           jsonResult.setData(byAodLike);
           jsonResult.setCode("200");
           jsonResult.setMsg("检索成功");
       }catch (Exception e){
           e.printStackTrace();
           jsonResult.setCode("406");
           jsonResult.setMsg("系统开小差了");
       }
        return jsonResult.getValues();
    }

    /**
     * 查询所有商品单位下拉框
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/selectByUnit",method = RequestMethod.POST)
    public Map<String,Object> selectByUnit(){
        JsonResult jsonResult=new JsonResult();
        List<Map<String, Object>> largeUnit = addOrderDataService.selectByUnit();
        if (largeUnit!=null && !largeUnit.equals("")) {
            jsonResult.setCode("200");
            jsonResult.setMsg("检索成功");
            jsonResult.setData(largeUnit);
        }else{
            jsonResult.setCode("501");
            jsonResult.setMsg("系统开小差了");
        }
        return jsonResult.getValues();
    }


    /**
     * 增加订单
     * @param map
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/insertAod",method = RequestMethod.POST)
    @ApiOperation(value = "新增销售订单接口",notes = "同时插入多条销售订单数据",httpMethod = "post",response = String.class)
    public Map insertAod(@RequestBody(required = false) Map<String,Object> map){
        JsonResult jsonResult=new JsonResult();
        try {
            Map xsDd = (Map) map.get("XsDd");
            String discount="0";
            xsDd.put("discount",discount);
            Timestamp time = new Timestamp(Calendar.getInstance().getTimeInMillis());
            xsDd.put("update_time",time);
            xsDdService.insert(xsDd);
            List<Map> addOrderDatas = (List<Map>) map.get("AddOrderDatas");
            for (Map addOrderData : addOrderDatas){
                addOrderDataService.insert(addOrderData);
                addOrderDataService.insert2(addOrderData);
            }
            jsonResult.setCode("200");
            jsonResult.setMsg("添加成功");
        }catch (Exception e){
            e.printStackTrace();
            jsonResult.setCode("501");
            jsonResult.setMsg("系统开小差了");
        }
        return jsonResult.getValues();
    }



    /**
     * 修改订单信息
     * @param map
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateAod",method = RequestMethod.POST)
    public Map updateAod(@RequestBody(required = false) Map<String,Object> map){
        JsonResult jsonResult=new JsonResult();
        try {
            Map xsDd = (Map) map.get("XsDd"); //取出销售订单的对象
            xsDdService.update(xsDd);
            List<Map> addOrderDatas = (List<Map>) map.get("AddOrderDatas");//取出商品订单的对象
            for (Map addOrderData : addOrderDatas){
                addOrderDataService.update(addOrderData);
            }
            jsonResult.setCode("200");
            jsonResult.setMsg("修改成功");
            jsonResult.setData(map);
        }catch (Exception e){
            e.printStackTrace();
            jsonResult.setCode("506");
            jsonResult.setMsg("系统开小差了");
        }
        return jsonResult.getValues();
    }




}
